package org.spring.springboot.app.community.member.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.agent.withdraw.domain.po.AccountWithdrawPO;
import org.spring.springboot.app.agent.withdraw.domain.vo.AccountWithdrawShowVO;
import org.spring.springboot.app.agent.withdraw.domain.vo.AccountWithdrawVO;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.community.member.domain.po.CommunityMemberPO;
import org.spring.springboot.app.community.member.domain.vo.*;
import org.spring.springboot.app.community.member.service.ICommunityMemberService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.spring.springboot.util.DateUtils;
import org.spring.springboot.util.excel.ExportExcel;
import org.spring.springboot.util.excel.ImportExcel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api(description="社区-社区成员表管理接口",tags = ApiIndex.CommunityMemberController)
@RequestMapping(value = "/api/communityMemberController")
@RestController
public class CommunityMemberController    {


    @Autowired
    private ICommunityMemberService service;

    @ApiOperation(value="查询社区成员列表")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.GET)
    public R<PageInfo<List<CommunityMemberVO>>> findAll(
            @ApiParam(value = "用户姓名", required = false) @RequestParam(defaultValue = "") String name,
            @ApiParam(value = "用户手机", required = false) @RequestParam(defaultValue = "") String mobile,
            @ApiParam(value = "用户类型（ 1业主  2.家人 3朋友）", required = false) @RequestParam(defaultValue = "1") String type,
            @ApiParam(value = "用户状态（1.启用 0.禁用）", required = false) @RequestParam(defaultValue = "") String state ,
            @ApiParam(value = "机构ID（如果传0 从后台的token里取）", required = false) @RequestParam(defaultValue = "0") String office_id,
            @ApiParam(value = "所属用户id", required = false) @RequestParam(defaultValue = "") String ownerUserId ,
            @ApiParam(value = "社区id", required = false) @RequestParam(defaultValue = "") String info_id,
            @ApiParam(value = "排序字段(需要与数据库字段对应)", required = false) @RequestParam(defaultValue = "") String sortField,
            @ApiParam(value = "排序方式（ascend正序,descend倒序）", required = false) @RequestParam(defaultValue = "") String sortOrder,
            @ApiParam(value = "开始创建时间（例如1993-08-11 或者 1993-08-11 00:00:00）", required = false) @RequestParam(defaultValue = "") String startCreateDate,
            @ApiParam(value = "结束创建时间（例如1993-08-11 或者 1993-08-11 23:59:59）", required = false) @RequestParam(defaultValue = "") String endCreateDate,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize
    ){
        SeachCommunityMemberVO vo = new SeachCommunityMemberVO();
        vo.setName(name);
        vo.setMobile(mobile);
        vo.setType(type);
        vo.setState(state);
        vo.setOfficeId(office_id);
        vo.setInfoId(info_id);
        vo.setSortField(sortField);
        vo.setSortOrder(sortOrder);
        vo.setOwnerUserId(ownerUserId);
        vo.setPageNum(pageNum);
        vo.setPageSize(pageSize);
        if(StringUtils.isNotBlank(startCreateDate)){
            if(startCreateDate.indexOf(":")==-1){
                startCreateDate=startCreateDate+" 00:00:00";
            }
        }
        if(StringUtils.isNotBlank(endCreateDate)){
            if(endCreateDate.indexOf(":")==-1){
                endCreateDate=endCreateDate+" 23:59:59";
            }
        }
        vo.setStartCreateDate(startCreateDate);
        vo.setEndCreateDate(endCreateDate);
        R<PageInfo<List<CommunityMemberVO>>> result = new R();
        List<CommunityMemberVO> _list = service.findAll(vo);
        PageInfo p = new PageInfo(_list);
        p.setList(_list);
        result.setData(p);
        return result;
    }

    @ApiOperation(value="根据ID得到社区成员")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public R<CommunityMemberVO> find(@ApiParam(value = "社区成员Id") @RequestParam(defaultValue = "") String id) {
        R<CommunityMemberVO> r = new R<CommunityMemberVO>();
        CommunityMemberPO po = service.find(id);
        CommunityMemberVO vo = new CommunityMemberVO();
        BeanUtils.copyProperties(po,vo);
        r.setData(vo);
        return r;
    }

    /**
     * 添加
     */
    @ApiOperation(value="添加社区成员")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(@ApiParam(value = "社区成员对象", required = true)@RequestBody SaveCommunityMemberVO vo) {
        R result =  service.save(vo);
        return result;
    }


    /**
     * 修改
     */
    @ApiOperation(value="修改社区成员")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public R update(@ApiParam(value = "社区成员对象", required = true) @RequestBody UpdateCommunityMemberVO vo) {
        R result =  service.update(vo);
        return result;
    }

    @ApiOperation(value="导出用戶Excel")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    public R exportExcel(
            @ApiParam(value = "request", hidden = true) HttpServletRequest request,
            @ApiParam(value = "response", hidden = true) HttpServletResponse response,
            @ApiParam(value = "楼栋ID") @RequestParam(defaultValue = "") String buildId
    ) {
        R r = new R();
        if (StringUtils.isBlank(buildId)) {
            return r.fail().setMessage("导出房间失败,大楼必须选择");
        }
        R checkR=service.checkBuildId(buildId);
        if(checkR.getCode()!=0){
            return r.fail().setMessage("大楼数据校验错误,请正确的选择大楼");
        }
        //得到房间的数据
        List<ExcelCommunityMemberVO> list = service.findExcelAllList(buildId);
        String fileName = checkR.getData() + "房间数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
        try {
            ExportExcel e =  new ExportExcel("在要添加业主的房间后面填写业主的姓名(必填)和手机(必填)", ExcelCommunityMemberVO.class).setDataList(list).write(response, fileName).dispose();
        } catch (Exception e) {
            return r.fail().setMessage("导出房间失败！失败信息:"+e.getMessage());
        }
        return r.success();
    }

    @ApiOperation(value="导入用戶Excel")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public R importExcel(@ApiParam(value = "导入的Excel", required = true) @RequestParam("file") MultipartFile file) {
        R r = new R();
        if (file.isEmpty()) {
            return r.fail().setMessage("文件为空");
        }
        try {
            ImportExcel ei = new ImportExcel(file, 1, 0);
            r=service.opExcelCommunityMember(ei.getDataList(ExcelCommunityMemberVO.class));
        } catch (Exception e) {
            return r.fail().setMessage("导出房间失败！失败信息:"+e.getMessage());
        }
        return r.success();
    }

}
